package cm.example.shoppingplatformspringboot.controller;


import cm.example.shoppingplatformspringboot.entity.Category;
import cm.example.shoppingplatformspringboot.entity.Goods;
import cm.example.shoppingplatformspringboot.entity.Manager;
import cm.example.shoppingplatformspringboot.entity.User;
import cm.example.shoppingplatformspringboot.service.AdminService;
import cm.example.shoppingplatformspringboot.utils.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cm.example.shoppingplatformspringboot.utils.ResponseUtil.generateResponse;

/**
 * @author 58211
 * 管理员控制器
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService adminService;


    // 检查用户信息是否完整
    private boolean isAdminInfoIncomplete(Manager manager) {
        return manager.getAccount() == null || manager.getAccount().isEmpty() ||
                manager.getPassword() == null || manager.getPassword().isEmpty();
    }

    //管理员登录
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> loginAdmin(@RequestBody Manager manager) {
        if (isAdminInfoIncomplete(manager)) {
            return  generateResponse("error", "请填写完整的信息", null);
        }
        Manager hasAdmin = adminService.login(manager.getAccount(), manager.getPassword(), manager.getLast_login_ip());
        if (hasAdmin == null) {
            return generateResponse("error", "账号或密码错误", null);
        }

        // 转化用户状态（0冻结，1正常，拉黑，3不存在）
        String status = hasAdmin.getStatus();
        // 转换状态描述
        String statusDesc = "0".equals(status) ? "冻结" : "正常";
        if (!"正常".equals(statusDesc)) {
            return generateResponse("error", "管理员已" + statusDesc + ",无法登录", null);
        }

        // 生成JWT Token
        String token = JwtUtil.generateAdminToken(manager);

        // 创建返回数据，包含用户信息和 token
        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        data.put("info", hasAdmin);  // 添加用户的全部信息
        return generateResponse("success", "登录成功", data);
    }

    //管理员注册
    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> registerUser(@RequestBody Manager manager) {
        // 检查用户信息是否完整
        if (isAdminInfoIncomplete(manager)) {
            return generateResponse("error", "请填写完整的信息", null);
        }

        // 检查用户是否已存在
        Manager hasAdmin = adminService.getAdminByAccount(manager.getAccount());
        if (hasAdmin != null) {
            return generateResponse("error", "该账号已存在", null);
        }
        // 如果用户不存在，执行注册操作
        int result = adminService.registerAdmin(manager);
        if (result > 0) {
            return generateResponse("success", "注册成功", null);
        } else {
            return generateResponse("error", "注册失败", null);
        }
    }

    // 添加管理员
    @PostMapping("/addAdmin")
    public ResponseEntity<Map<String, Object>> addAdmin(@RequestBody Manager manager) {
        // 检查账号是否存在
        if (adminService.isAccountExists(manager.getAccount())) {
            return generateResponse("warning", "账号已存在", null);
        }
        int result = adminService.addAdmin(manager);
        if (result > 0) {
            return generateResponse("success", "添加成功", null);
        } else {
            return generateResponse("error", "添加失败", null);
        }
    }

    //修改管理员
    @PutMapping("/editAdmin")
    public int editAdmin(@RequestBody Manager manager) {
        return adminService.editAdmin(manager);
    }

    //删除管理员
    @DeleteMapping("/deleteAdmin")
    public int deleteAdmin(@RequestParam int id) {
        return adminService.deleteAdmin(id);
    }

    //获取管理员列表
    @GetMapping("/getAdminList")
    public List<Manager> getAdminList() {
        return adminService.getAdminList();
    }

    //管理员禁用管理员
    @PutMapping("/disableAdmin")
    public int disableAdmin(@RequestParam int id) {
        return adminService.disableAdmin(id);
    }

    //管理员解禁管理员
    @PutMapping("/unDisableAdmin")
    public int unDisableAdmin(@RequestParam int id) {
        return adminService.unDisableAdmin(id);
    }

    //获取用户列表
    @GetMapping("/getUserList")
    public List<User> getUserList() {
        return adminService.getUserList();
    }

    //删除用户
    @DeleteMapping("/deleteUser")
    public int deleteUser(@RequestParam int id) {
        return adminService.deleteUser(id);
    }

    //拉黑用户
    @PutMapping("/blockUser")
    public int blockUser(@RequestParam int id) {
        return adminService.blockUser(id);
    }

    //解除拉黑
    @PutMapping("/unblockUser")
    public int unblockUser(@RequestParam int id) {
        return adminService.unblockUser(id);
    }

    //冻结用户
    @PutMapping("/freezeUser")
    public int freezeUser(@RequestParam int id) {
        return adminService.freezeUser(id);
    }

    //解冻用户
    @PutMapping("/unfreezeUser")
    public int unfreezeUser(@RequestParam int id) {
        return adminService.unfreezeUser(id);
    }

    //添加商品类型
    @PostMapping("/addProductType")
    public int addProductType(@RequestParam String name) {
        return adminService.addProductType(name);
    }

    //获取商品类型列表
    @GetMapping("/getProductTypeList")
    public List<Category> getProductTypeList() {
        return adminService.getProductTypeList();
    }

    //删除商品类型
    @DeleteMapping("/deleteProductType")
    public int deleteProductType(@RequestParam int id) {
        return adminService.deleteProductType(id);
    }

    // 商品列表
    @GetMapping("/goodsList")
    public List<Goods> goodsList() {
        return adminService.selectAll();
    }

    // 所有商品类型
    @GetMapping("/getAllShopGenres")
    public List<Map<String, Object>> getAllShopGenres() {
        return adminService.getAllShopGenres();
    }

    // 根据商品类型获取商品列表
    @GetMapping("/getGoodsListByGenre")
    public List<Goods> getGoodsListByGenre(@RequestParam String value) {
        return adminService.getGoodsListByGenre(value);
    }

    // 搜索商品
    @GetMapping("/searchGoods")
    public List<Goods> searchGoods(@RequestParam String input) {
        return adminService.searchGoods(input);
    }

    // 搜索商品名称
    @GetMapping("/searchGoodsByName")
    public List<String> searchGoodsByName(@RequestParam String name) {
        return adminService.searchGoodsByName(name);
    }

    //删除商品
    @DeleteMapping("/deleteGoods")
    public int adminDeleteGoods(@RequestParam int id) {
        return adminService.deleteGoods(id);
    }

    //下架商品
    @PutMapping("/soldOutGoods")
    public int soldOutGoods(@RequestParam int id) {
        return adminService.soldOutGoods(id);
    }

    //上架商品
    @PutMapping("/putOnShelfGoods")
    public int putOnShelfGoods(@RequestParam int id) {
        return adminService.putOnShelfGoods(id);
    }

    //添加商品
    @PostMapping("/addGoods")
    public int addGoods(@RequestBody Goods goods) {
        return adminService.addGoods(goods);
    }

    //修改商品
    @PutMapping("/editGoods")
    public int editGoods(@RequestBody Goods goods) {
        return adminService.editGoods(goods);
    }
}
